<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card style="margin-top:10px">
      <el-button
        style="margin-bottom:15px"
        type="primary"
        @click="handleSelect(),dialogVisible=true"
      >
        添加用户
      </el-button>
      <!-- 表格 -->

      <tree-table
        :data="categoriesList"
        :expand-type="false"
        :selection-type="false"
        show-index
        class="treetable"
        index-text="#"
        stripe
        border
        :show-row-hover="false"
        :columns="columns"
      >

        <!-- 是否有效 -->
        <template
          slot="isok"
          slot-scope="scope"
        >
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color:lightgreen"
          ></i>
          <i
            class="el-icon-error"
            v-else-if="scope.row.cat_deleted === true"
            style="color:lightred"
          ></i>
        </template>
        <!-- 排序 -->
        <template
          slot="order"
          slot-scope="scope"
        >
          <el-tag v-if="scope.row.cat_level ===0 ">一级</el-tag>
          <el-tag
            type="success"
            v-else-if="scope.row.cat_level === 1 "
          >二级</el-tag>
          <el-tag
            type="info"
            v-else-if="scope.row.cat_level ===2"
          >三级</el-tag>
        </template>
        <template
          slot="post"
          slot-scope="scope"
        >
          <el-button
            type="primary"
            icon="el-icon-edit"
            @click="bianji(scope.row),xxx=true"
          >编辑</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="shanchu(scope.row)"
          >删除</el-button>
        </template>

      </tree-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
        style="margin-top:15px"
      >
      </el-pagination>

      <!-- 添加框 -->
      <el-dialog
        title="添加用户"
        :visible.sync="dialogVisible"
        width="30%"
        @close="addClosed"
      >
        <el-form
          :model="xiugaiList"
          :rules="rules"
          ref="xiugaiList"
          label-width="100px"
        >
          <el-form-item
            label="分类名称"
            prop="cat_name"
          >
            <el-input v-model="xiugaiList.cat_name"></el-input>
          </el-form-item>

          <el-form-item label="父级分类">
            <el-cascader
              :props="cascaderprops"
              :options="parentList"
              v-model="selectKyes"
              @change="selectList"
              clearable
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span
          slot="footer"
          class="dialog-footer"
        >
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="addcate()"
          >确 定</el-button>
        </span>
      </el-dialog>

      <!-- 编辑 -->
      <el-dialog
        title="修改信息"
        :visible.sync="xxx"
        width="30%"
      >
        <el-form
          :model="fenlei"
          label-width="80px"
        >
          <el-form-item
            label="分类名称"
            prop="cat_name"
          >
            <el-input v-model="fenlei.cat_name"></el-input>
          </el-form-item>

        </el-form>
        <span
          slot="footer"
          class="dialog-footer"
        >
          <el-button @click="xxx = false">取 消</el-button>
          <el-button
            type="primary"
            @click="tijiao(),xxx = false"
          >确 定</el-button>
        </span>
      </el-dialog>

    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      xxx: false,
      parentList: [],
      dialogVisible: false,
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      xiugaiList: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0,
      },
      categoriesList: [],
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          type: "template",
          template: "isok",
        },
        {
          label: "排序",
          type: "template",
          template: "order",
        },
        {
          label: "操作",
          type: "template",
          template: "post",
        },
      ],
      rules: {
        cat_name: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },
      cascaderprops: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      selectKyes: [],
      fenlei: {},
    };
  },
  created() {
    this.getcateList();
  },
  methods: {
    //获取所有分类
    async getcateList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品分类失败!");
      }
      // 把数据列表赋值给cateList
      this.categoriesList = res.data.result;
      // 获取商品的总数量
      this.total = res.data.total;
    },
    //获取一级二级分类
    async handleSelect() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 },
      });
      this.parentList = res.data;
      console.log(this.parentList);
      // console.log(res);
    },
    //查找下标id及等级id
    selectList() {
      if (this.selectKyes.length > 0) {
        this.xiugaiList.cat_pid = this.selectKyes[this.selectKyes.length - 1];
        this.xiugaiList.cat_level = this.selectKyes.length;
        return;
      } else {
        this.xiugaiList.cat_pid = 0;
        this.xiugaiList.cat_level = 0;
      }
      // console.log(this.selectKyes);
    },
    //分页
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getcateList();
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getcateList();
    },
    //添加分类
    addcate() {
      this.$refs.xiugaiList.validate(async (valid) => {
        if (!valid) return;
        const res = await this.$http.post("categories", this.xiugaiList);
        console.log(res);
        if (res.data.meta.status === 201) {
          this.$message.success("添加成功");
        } else {
          this.$message.error("添加失败");
        }
      });
      this.getcateList();
      this.dialogVisible = false;
    },

    addClosed() {
      this.$refs.xiugaiList.resetFields();
      this.selectKyes = [];
      this.xiugaiList.cat_pid = 0;
      this.xiugaiList.cat_level = 0;
    },

    //根据id查名称
    async bianji(cateInfo) {
      const res = await this.$http.get(`categories/${cateInfo.cat_id}`);
      this.fenlei = res.data.data;
    },
    //编辑
    async tijiao() {
      const res = await this.$http.put("categories/" + this.fenlei.cat_id, {
        cat_name: this.fenlei.cat_name,
      });
      if (res.data.meta.status !== 200) return this.$message.error("编辑失败");
      this.$message.success("编辑成功");
      this.getcateList();
    },
    //删除
    async shanchu(cateInfo) {
      this.$confirm("此操作将删除该分类, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await this.$http.delete(`categories/${cateInfo.cat_id}`);
          // console.log(res.data);
          this.getcateList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped>
</style>
